package com.smap.layer;

import com.smap.core.Bbox;

public class WmsLayer extends GridLayer {

	public boolean singleTile = false;
	public String format = "png";
	public String server;
	public String layer;

	public WmsLayer(String server, String layer) {
		super();
		this.server = server;
		this.layer = layer;
	}

	public void draw() {
		if (singleTile) {
			clearScreen();
			Bbox extent = this.map.getExtent(this.width, this.height);

			String url = server
					+ "?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&transparent=true"
					+ "&LAYERS=" + layer + "&SRS="
					+ this.map.getProj().getSRS() + "&BBOX=" + extent
					+ "&WIDTH=" + this.width + "&HEIGHT=" + this.height
					+ "&FORMAT=image/" + this.format;

			drawPicture(url, 0, 0);
		} else {
			super.draw();
		}
	}

	@Override
	public void setUrl(Tile tile) {
		double res = map.getResolution();
		double x = this.minX + (tile.x * tileSize) * res;
		double y = this.minX + (tile.y * tileSize) * res;
		Bbox extent = new Bbox().expand(x, y).expand(x + tileSize * res,
				y + tileSize * res);
		String url = server
				+ "?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&transparent=true"
				+ "&LAYERS=" + layer + "&SRS="
				+ this.map.getProj().getSRS() + "&BBOX=" + extent
				+ "&WIDTH=" + tileSize + "&HEIGHT=" + tileSize
				+ "&FORMAT=image/" + this.format;
		// System.out.println(url);
		tile.imageUrl = url;
	}

}
